/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormLaporanDataMobil2.java
 *
 * Created on 18 Des 11, 18:37:27
 */
package rentalmobil.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import rentalmobil.entity.Laporan_mobil;
import rentalmobil.entity.dataMobil;

/**
 *
 * @author User
 */
public class FormLaporanDataMobil2 extends javax.swing.JDialog {

    Laporan_mobil lap = new Laporan_mobil();
    dataMobil dataM = new dataMobil();
    List<dataMobil> datamobil = new ArrayList<dataMobil>();
    List<Laporan_mobil> latih = new ArrayList<Laporan_mobil>();

    /** Creates new form FormLaporanDataMobil2 */
    public FormLaporanDataMobil2(java.awt.Frame parent, boolean modal) throws SQLException {
        super(parent, modal);
        initComponents();
        Laporan_mobil dta = new Laporan_mobil();
        this.loadNoPlat();
        this.isiTabelLaporan();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = this.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);

    }

    public void loadNoPlat() throws SQLException {
        comboNoPlatMobil.removeAllItems();
        datamobil = dataM.viewDataNoMobil();
        for (dataMobil d : datamobil) {
            comboNoPlatMobil.addItem(d.getNo_plat_mobil());
        }
    }

    void hapusTextField() {

        txtKondisiMobil.setText(null);
////        txtTglMulaiServis.setText(null);
//        txtTglSelesaiServis.setText(null);
        txtBiayaServis.setText(null);
        txtKeteranganField.setText(null);

    }

    void isiTabelLaporan() throws SQLException {
        latih = lap.viewTabelLaporanData();
        Object data[][] = new Object[latih.size()][6];
        int x = 0;
        for (Laporan_mobil d : latih) {
            data[x][0] = d.getNo_laporan();
            data[x][1] = d.getNo_plat_mobil();
            data[x][2] = d.getTgl_servis();
            data[x][3] = d.getTgl_selesai();
            data[x][4] = d.getBiaya_servis();
            data[x][5] = d.getKeterangan();
            x++;
        }
        String judul[] = {"No Laporan", "No Mobil", "Tanggal Servis", "Tanggal Selesai", "Biaya", "Keterangan"};
        LaporanTable.setModel(new DefaultTableModel(data, judul));
        jScrollPane1.setViewportView(LaporanTable);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        btnSimpan = new javax.swing.JButton();
        resetbutton = new javax.swing.JButton();
        btnKeluar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        btn_Cek = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtKondisiMobil = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtBiayaServis = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtKeteranganField = new javax.swing.JTextField();
        comboNoPlatMobil = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        ServisMulaiDate = new com.toedter.calendar.JDateChooser();
        serviceselesai = new com.toedter.calendar.JDateChooser();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        LaporanTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel4.setBackground(new java.awt.Color(204, 255, 204));

        jPanel3.setBackground(new java.awt.Color(153, 255, 153));
        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        btnSimpan.setText("Simpan");
        btnSimpan.setToolTipText("Simpan");
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });

        resetbutton.setText("Reset");
        resetbutton.setToolTipText("Reset");
        resetbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetbuttonActionPerformed(evt);
            }
        });

        btnKeluar.setText("Keluar");
        btnKeluar.setToolTipText("Keluar");
        btnKeluar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKeluarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(174, 174, 174)
                .addComponent(btnSimpan, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addComponent(resetbutton)
                .addGap(18, 18, 18)
                .addComponent(btnKeluar)
                .addContainerGap(133, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnKeluar, btnSimpan, resetbutton});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnKeluar)
                    .addComponent(btnSimpan)
                    .addComponent(resetbutton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(153, 255, 153));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Isi Data Keterangan Mobil", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 12))); // NOI18N

        jLabel2.setText("id Mobil");

        btn_Cek.setText("Cek");
        btn_Cek.setToolTipText("Cek");
        btn_Cek.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CekActionPerformed(evt);
            }
        });

        jLabel3.setText("Kondisi Mobil");

        txtKondisiMobil.setEnabled(false);

        jLabel4.setText("tgl mulai servis");

        jLabel5.setText("tgl selesai servis");

        jLabel6.setText("biaya servis");

        jLabel7.setText("Keterangan");

        comboNoPlatMobil.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel8.setText("(Turun mesin, ganti oli, dan body pecah)");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(120, 120, 120)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtKeteranganField, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtBiayaServis, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                            .addComponent(serviceselesai, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                            .addComponent(txtKondisiMobil, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                            .addComponent(comboNoPlatMobil, javax.swing.GroupLayout.Alignment.LEADING, 0, 137, Short.MAX_VALUE)
                            .addComponent(ServisMulaiDate, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_Cek, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)))
                .addGap(203, 203, 203))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(comboNoPlatMobil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_Cek))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtKondisiMobil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(ServisMulaiDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(serviceselesai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(9, 9, 9)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(txtBiayaServis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtKeteranganField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8))
                    .addComponent(jLabel7))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBackground(new java.awt.Color(153, 255, 153));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("Laporan Data Mobil");
        jPanel1.add(jLabel1);

        LaporanTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "no plat mobil", "kondisi mobil", "tgl mulai servis", "tgl selesai servis", "biaya servis"
            }
        ));
        jScrollPane1.setViewportView(LaporanTable);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 628, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 628, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel4, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimpanActionPerformed
        Laporan_mobil km = new Laporan_mobil();
        try {
            if (txtKeteranganField.getText().isEmpty() || txtBiayaServis.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this, "Isi Data Secara Lengkap", "Peringatan", JOptionPane.ERROR_MESSAGE);
            } else {
                if (txtKondisiMobil.getText().equals("Rusak")) {
                    km.setNo_plat_mobil(comboNoPlatMobil.getSelectedItem().toString());
                    km.setTgl_servis(new java.sql.Date(ServisMulaiDate.getDate().getTime()));
                    km.setTgl_selesai(new java.sql.Date(serviceselesai.getDate().getTime()));
                    km.setBiaya_servis(txtBiayaServis.getText());
                    km.setKeterangan(txtKeteranganField.getText());
                    km.insertLaporanMobil();
                    dataM.updateKondisiMobil(comboNoPlatMobil.getSelectedItem().toString());
                    JOptionPane.showMessageDialog(this, "Data Sudah Tersimpan");
                    this.hapusTextField();
                    this.isiTabelLaporan();
                } else {
                    JOptionPane.showMessageDialog(this, "Mobil Masih Bagus", "Peringatan", JOptionPane.ERROR_MESSAGE);
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(FormLaporanDataMobil2.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_btnSimpanActionPerformed

    private void resetbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetbuttonActionPerformed
        this.hapusTextField();
}//GEN-LAST:event_resetbuttonActionPerformed

    private void btnKeluarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnKeluarActionPerformed
        this.dispose();
}//GEN-LAST:event_btnKeluarActionPerformed

    private void ServisMulaiDatePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_ServisMulaiDatePropertyChange
//       serviceselesai.setmi;
    }//GEN-LAST:event_ServisMulaiDatePropertyChange

    private void btn_CekActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CekActionPerformed
        dataMobil tes = new dataMobil();
        try {
            tes.LaporanMobilView(comboNoPlatMobil.getSelectedItem().toString());
            txtKondisiMobil.setText(tes.getKondisi_Mobil());
        } catch (SQLException ex) {
            Logger.getLogger(FormLaporanDataMobil2.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_btn_CekActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                FormLaporanDataMobil2 dialog = null;
                try {
                    dialog = new FormLaporanDataMobil2(new javax.swing.JFrame(), true);
                } catch (SQLException ex) {
                    Logger.getLogger(FormLaporanDataMobil2.class.getName()).log(Level.SEVERE, null, ex);
                }
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable LaporanTable;
    private com.toedter.calendar.JDateChooser ServisMulaiDate;
    private javax.swing.JButton btnKeluar;
    private javax.swing.JButton btnSimpan;
    private javax.swing.JButton btn_Cek;
    private javax.swing.JComboBox comboNoPlatMobil;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton resetbutton;
    private com.toedter.calendar.JDateChooser serviceselesai;
    private javax.swing.JTextField txtBiayaServis;
    private javax.swing.JTextField txtKeteranganField;
    private javax.swing.JTextField txtKondisiMobil;
    // End of variables declaration//GEN-END:variables
}
